<script lang="ts" setup>
import { getCurrentInstance, reactive, ref } from 'vue'
import { onLoad, onPageScroll, onReady } from '@dcloudio/uni-app'
import { tnNavPage } from '@tuniao/tnui-vue3-uniapp/utils'
import TnButton from '@tuniao/tnui-vue3-uniapp/components/button/src/button.vue'
import type { NavbarRectInfo } from '@tuniao/tnui-vue3-uniapp/components/navbar'
import PageContainer from '@/components/page-container/index.vue'
import useZPaging from '@/uni_modules/z-paging/components/z-paging/js/hooks/useZPaging.js'
import { useScrollTransparentNavbar } from '@/hooks'
import circles from '@/api/circles.js'
import { FILE_URL } from '@/api/config.js'

const paging = ref(null)
useZPaging(paging)
// 导航栏的高度
const navbarHeight = ref(0)
const instance = getCurrentInstance()
const state = reactive({
  circlesId: 0,
  lists: [],
})

const { init: initTransparentScroll, opacityScrollHandle } =
  useScrollTransparentNavbar(instance)

// 导航栏初始化完毕
const navbarInitFinishHandle = (info: NavbarRectInfo) => {
  navbarHeight.value = info.height
}
const navUserHomePage = (uId: number) => {
  tnNavPage(`/pagesA/userhome?otherUserId=${uId}`)
}

const queryList = (pageNo: number, pageSize: number) => {
  circles
    .circlesMemberApplying({
      page: pageNo,
      page_size: pageSize,
      circles_id: state.circlesId,
    })
    .then((res) => {
      paging.value.complete(res.data)
    })
    .catch((err: any) => {
      console.log(err, 'lists')
      paging.value.complete(false)
    })
}
const circlesMemberAudit = (operateUserId: number) => {
  circles
    .auditCirclesMember({
      circles_id: state.circlesId,
      audit_user_id: operateUserId,
      status: '1',
    })
    .then((res) => {
      uni.showToast({
        title: res.msg,
        icon: 'none',
      })
      if (res.code == 1) {
        paging.value.reload(true)
      }
    })
    .catch((err: any) => {
      console.log(err, 'circlesMemberAudit')
    })
}
const removeCirclesMember = (operateUserId: number) => {
  circles
    .circlesMemberRemove({
      circles_id: state.circlesId,
      remove_user_id: operateUserId,
    })
    .then((res) => {
      uni.showToast({
        title: res.msg,
        icon: 'none',
      })
      if (res.code == 1) {
        paging.value.reload(true)
      }
    })
    .catch((err: any) => {
      console.log(err, 'removeCirclesMember')
    })
}
onLoad((options) => {
  if (!options?.circlesId) {
    uni.showModal({
      title: '提示',
      content: '参数错误',
      showCancel: false,
      success: () => {
        uni.navigateBack()
      },
    })
    return
  }
  state.circlesId = options.circlesId
})
onReady(() => {
  initTransparentScroll()
})
onPageScroll((e) => {
  opacityScrollHandle(e.scrollTop)
})
</script>

<template>
  <PageContainer :placeholder-bottom="true">
    <TnNavbar
      fixed
      :bottom-shadow="false"
      :placeholder="false"
      @init-finish="navbarInitFinishHandle"
    >
      成员申请
    </TnNavbar>
    <view class="container">
      <view class="tn-mt-sm">
        <z-paging
          ref="paging"
          v-model="state.lists"
          :paging-style="{ paddingTop: `${navbarHeight + 10}px` }"
          :safe-area-inset-bottom="true"
          :use-safe-area-placeholder="true"
          @query="queryList"
        >
          <view class="">
            <view
              v-for="(item, index) in state.lists"
              :key="index"
              class="tn-flex user-lists items-center"
            >
              <view class="" @click="navUserHomePage(item.user.id)">
                <view class="logo-pic">
                  <view class="logo-image">
                    <view
                      class=""
                      :style="
                        'background-image:url(' +
                        FILE_URL +
                        item.user.avatar +
                        ');width: 90rpx;height: 90rpx;background-size: cover;'
                      "
                    />
                  </view>
                </view>
              </view>
              <view class="tn-pl-sm" style="width: 50vw">
                <view class="tn-flex justify-between tn-text-ellipsis-1">
                  <view
                    class="justify-content-item"
                    @click="navUserHomePage(item.user.id)"
                  >
                    <text class="tn-text-lg">
                      {{ item.user.nickname }}
                    </text>
                  </view>
                </view>
                <view class="tn-pt-xs tn-text-ellipsis tn-text-ellipsis-1">
                  <text class="tn-color-gray tn-text-sm">
                    申请时间：{{ item.create_time }}
                  </text>
                </view>
                <view
                  v-if="item.remark != ''"
                  class="tn-pt-xs tn-text-ellipsis tn-text-ellipsis-1"
                >
                  <text class="tn-color-gray tn-text-sm">
                    {{ item.remark }}
                  </text>
                </view>
              </view>
              <view class="" style="width: 30vw">
                <view class="tn-flex justify-end tn-mb-xs tn-mt-xs">
                  <TnButton
                    type="danger"
                    @click="removeCirclesMember(item.user.id)"
                    >拒绝</TnButton
                  >
                  <TnButton
                    type="primary"
                    :custom-style="{ margin: '0 0 0 10rpx' }"
                    @click="circlesMemberAudit(item.user.id)"
                  >
                    同意
                  </TnButton>
                </view>
              </view>
            </view>
          </view>
        </z-paging>
      </view>
      <view class="tn-tabbar-height" />
    </view>
  </PageContainer>
</template>

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  background: #f8f7f8;

  .user-lists {
    background-color: #ffffff;
    border-radius: 18rpx;
    margin: 20rpx 30rpx;
    padding: 20rpx;
  }
}

/* 用户头像 start */
.logo-image {
  width: 90rpx;
  height: 90rpx;
  position: relative;
}

.logo-pic {
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  border-radius: 50rpx;
  overflow: hidden;
}

/* 底部安全边距 start*/
.tn-tabbar-height {
  min-height: 120rpx;
  height: calc(140rpx + env(safe-area-inset-bottom));
  height: calc(140rpx + constant(safe-area-inset-bottom));
}
</style>
